﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace buildingLib
{
    public class DBHelper
    {
        public static string strCon;

        /// <summary>
        /// 给以插入到数据库中的文件(rasterfile)，添加标记，以区重复的文件名
        /// </summary>
        /// <param name="hashtable">内容是文件名为key，以标记为内容</param>
        public void UpdateRasterFileName(Hashtable hashtable,string ext)
        {
            foreach (DictionaryEntry dicEntry in hashtable)
            {
                string filename = dicEntry.Key.ToString() + dicEntry.Value.ToString();
                SqlConnection conn = GetConn(DBHelper.strCon);
                string sqls = "update rasterfile set filemark='" + dicEntry.Value.ToString() + "' ,ext='" + ext + "' " + 
                    "where filemark='none' and name='" + dicEntry.Key.ToString() + "'";
                SqlCommand comm = new SqlCommand(sqls, conn);
                comm.ExecuteNonQuery();
                conn.Close();
                comm.Dispose();
            }
        }

        /// <summary>
        /// 数据库连接
        /// </summary>
        /// <param name="sqlcon">数据库连接字符串</param>
        /// <returns>数据库连接对象</returns>
        public SqlConnection GetConn(string sqlcon)
        {
            SqlConnection conn = new SqlConnection(sqlcon);
            conn.Open();
            return conn;
        }

        /// <summary>
        /// 根据交费时间取得模版
        /// </summary>
        /// <param name="mark"></param>
        /// <returns></returns>
        public Guid GetTemplateId(string mark)
        {
            SqlConnection conn = GetConn(DBHelper.strCon);
            string sqls = "select feetemplateid from k_feelog where payedtime='" + mark + "'";
            SqlCommand com = new SqlCommand(sqls, conn);
            SqlDataReader reader = com.ExecuteReader();
            Guid templateid = (Guid)reader["feetemplateid"];
            return templateid;
        }
    }
}
